package com.ruoyi.web.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.admin.domain.vo.request.profit.ProfitAddRequest;
import com.ruoyi.admin.domain.vo.request.profit.ProfitPageJoinRequest;
import com.ruoyi.admin.domain.vo.request.profit.ProfitPageRequest;
import com.ruoyi.admin.domain.vo.request.profit.ProfitUpdateRequest;
import com.ruoyi.admin.domain.vo.response.profit.ProfitPageResponse;
import com.ruoyi.admin.domain.vo.response.profit.ProfitResponse;
import com.ruoyi.admin.service.ProfitService;
import com.ruoyi.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 报表(Profit)Controller类
 *
 * @author jianhan
 * @date 2022-12-07 15:41:39
 */
@Api(value = "后台报表 Controller", tags = {"后台报表"})
@RestController
@RequestMapping("/api/profit")
public class ProfitController {

    @Autowired
    private ProfitService profitService;

    /**
     * 根据id获取数据
     *
     * @author jianhan
     * @date 2022-12-07 15:41:39
     **/
    @ApiOperation(value = "根据id获取数据", notes = "根据id获取数据")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public Result<ProfitResponse> get(@PathVariable Long id) {
        return profitService.getId(id);
    }


    /**
     * 分页获取报表列表
     *
     * @author jianhan
     * @date 2022-12-07 15:41:39
     **/
    @ApiOperation(value = "分页获取报表列表", notes = "分页获取报表列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "查询数量", paramType = "query", dataType = "Integer")
    })
    @GetMapping
    public Result<ProfitPageResponse> pageList(@Valid ProfitPageJoinRequest req) {
        return profitService.page(new Page<>(req.getPageNum(), req.getPageSize()), req);
    }


    /**
     * 添加报表
     *
     * @author jianhan
     * @date 2022-12-07 15:41:39
     **/
    @ApiOperation(value = "添加报表", notes = "添加报表")
    @PostMapping
    public Result add(@RequestBody @Valid ProfitAddRequest req) {
        return profitService.add(req.getProfitModel());
    }


    /**
     * 修改报表
     *
     * @author jianhan
     * @date 2022-12-07 15:41:39
     **/
    @ApiOperation(value = "修改报表", notes = "修改报表")
    @PutMapping()
    public Result update(@RequestBody @Valid ProfitUpdateRequest req) {
        return profitService.update(req.getProfitModel());
    }


    /**
     * 删除报表
     *
     * @author jianhan
     * @date 2022-12-07 15:41:39
     **/
    @ApiOperation(value = "删除报表", notes = "删除报表")
    @ApiImplicitParam(name = "ids", value = "id集合,逗号隔开", required = true, paramType = "query", dataType = "String")
    @DeleteMapping("/{ids}")
    public Result update(@PathVariable String ids) {
        Boolean result = profitService.removeByIds(Arrays.asList(ids.split(",")).stream().collect(Collectors.toList()));
        return Result.judge(result);
    }

}
